package com.aspose.words.maven.examples;

import java.io.File;
import java.net.URI;
import java.util.LinkedList;
import java.util.Queue;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import com.aspose.words.Activator;
import com.aspose.words.maven.utils.AsposeConstants;
import com.aspose.words.maven.utils.AsposeJavaAPI;
import com.aspose.words.maven.utils.AsposeMavenProjectManager;
import com.aspose.words.maven.utils.AsposeWordsJavaAPI;
import com.aspose.words.maven.utils.FormatExamples;

public class AsposeExampleWizardPage extends WizardPage {

	private Combo cbProject;
	private Combo cbVersion;
	private Tree examplesTree;

	/**
	 * Create the wizard.
	 */
	public AsposeExampleWizardPage() {
		super("wizardPage");
		setTitle("New Aspose.Words Code Example");
		setDescription("Aspose.Words Java API - Code Examples");
	}

	public IProject getIProject() {
		return (IProject) cbProject.getData(cbProject.getText());
	}

	public String getSelectedProjectPath() {
		String projectPath = null;
		try {
			IProject project = (IProject) cbProject.getData(cbProject.getText());
			projectPath = project.getDescription().getLocationURI().getPath();

		} catch (CoreException e) {
			e.printStackTrace();
		}
		return projectPath;
	}

	public String getSelectedProjectName() {
		return cbProject.getText();
	}

	public String getSelectedExampleCategory() {
		return examplesTree.getSelection()[0].getText().trim().replace(' ', '_').toLowerCase();
	}

	private void initControls() {

		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		URI path = null;
		String versionNo = null;
		IProject selectedProject = null;

		try {

			// init cbProject
			for (IProject project : projects) {
				path = project.getDescription().getLocationURI();
				if (path != null) {
					versionNo = AsposeMavenProjectManager.getInstance().getDependencyVersionFromPOM(path,
							AsposeConstants.API_MAVEN_DEPENDENCY);
					if (versionNo != null) {
						cbProject.add(project.getDescription().getName());
						cbProject.setData(project.getDescription().getName(), project);
					}
				}
			}
			cbProject.select(0);

			// init cbVersion
			selectedProject = (IProject) cbProject.getData(cbProject.getText());
			cbVersion.removeAll();
			versionNo = AsposeMavenProjectManager.getInstance().getDependencyVersionFromPOM(
					selectedProject.getDescription().getLocationURI(), AsposeConstants.API_MAVEN_DEPENDENCY);
			if (versionNo != null) {
				cbVersion.add(versionNo);
				cbVersion.select(0);
			}

			// init tree
			AsposeMavenProjectManager.initialize(new File(selectedProject.getDescription().getLocationURI()));
			AsposeJavaAPI component = AsposeWordsJavaAPI.initialize(AsposeMavenProjectManager.getInstance());
			component.checkAndUpdateRepo(); // new line
			if (component.isExamplesDefinitionAvailable()) {
				populateExamplesTree(component);
			}

		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 *
	 * @param asposeComponent
	 * @param top
	 * @param panel
	 */
	public void populateExamplesTree(AsposeJavaAPI asposeComponent) {
		String examplesFullPath = asposeComponent.getLocalRepositoryPath() + File.separator
				+ AsposeConstants.GITHUB_EXAMPLES_SOURCE_LOCATION;
		File directory = new File(examplesFullPath);
		examplesTree.removeAll();
		Queue<Object[]> queue = new LinkedList<>();
		queue.add(new Object[] { null, directory });
		TreeItem top = new TreeItem(examplesTree, 0);
		top.setText("Aspose.Words");

		while (!queue.isEmpty()) {
			Object[] _entry = queue.remove();
			File childFile = ((File) _entry[1]);
			TreeItem parentItem = (TreeItem) _entry[0];
			if (childFile.isDirectory()) {
				if (parentItem != null) {
					TreeItem child = new TreeItem(parentItem, SWT.NONE);
					child.setText(FormatExamples.formatTitle(childFile.getName()));
					parentItem = child;
				} else {
					parentItem = top;
				}
				for (File f : childFile.listFiles()) {
					queue.add(new Object[] { parentItem, f });
				}
			} else if (childFile.isFile()) {
				TreeItem child = new TreeItem(parentItem, SWT.NONE);
				child.setText(FormatExamples.formatTitle(childFile.getName()));
			}
		}
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);

		cbProject = new Combo(container, SWT.READ_ONLY);
		cbProject.setBounds(181, 101, 366, 23);

		examplesTree = new Tree(container, SWT.BORDER);
		examplesTree.setBounds(27, 170, 520, 140);

		Label label = new Label(container, SWT.NONE);
		label.setImage(SWTResourceManager.getImage(Activator.getResourceFilePath("long_banner.png")));
		label.setBounds(10, 0, 564, 95);

		Label lblProject = new Label(container, SWT.NONE);
		lblProject.setBounds(134, 104, 40, 15);
		lblProject.setText("Project:");

		Label lblAsposewordsForJava = new Label(container, SWT.NONE);
		lblAsposewordsForJava.setBounds(5, 131, 170, 15);
		lblAsposewordsForJava.setText("Aspose.Words for Java (version):");

		cbVersion = new Combo(container, SWT.READ_ONLY);
		cbVersion.setBounds(181, 128, 366, 23);

		initControls();
	}

}
